<?php

namespace App\Http\Controller\Test;

use Swoft\Http\Message\Request;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Swoft\Http\Server\Annotation\Mapping\RequestMethod;
use ServiceComponents\Enum\HostEnum;

/**
 * Class EsController
 * @Controller(prefix="/es")
 */
class EsController extends BaseController {

    /**
     * 添加商品
     * @RequestMapping(route="addGoods",method={RequestMethod::POST})
     */
    public function addGoods(Request $request){
        $params = $request->input();

        $client = \Elasticsearch\ClientBuilder::create()->setHosts(config('Es'))->setBasicAuthentication('elastic','changeme')->build();
        /*这里写死数据，实际场景中，根据需求替换成自己的数据*/
        $params = [
            'index' => 'goods_list',
            'id'    => (int)$params['id'],
            'body'  => [
                'id'     => $params['id'],
                'goods_name' => $params['goods_name'],
                'goods_description'    => $params['goods_description'],
                'goods_info' => $params['goods_info'],
            ]
        ];
        $response = $client->index($params);
        return $response;
    }

    /**
     * 搜索商品
     * @RequestMapping(route="searchGoods",method={RequestMethod::GET})
     */
    public function searchGoods(Request $request){
        $params = $request->input();

        $client = \Elasticsearch\ClientBuilder::create()->setHosts(config('Es'))->setBasicAuthentication('elastic','changeme')->build();
        /*这里写死数据，实际场景中，同学们根据需求替换成自己的数据*/
        $params = [
            'index' => 'goods_list',
            'body'  => [
                'query' => [
                    'match' => [
                        'goods_name' => $params['goods_name']
                    ]
                ]
            ]
        ];
        $response = $client->search($params);
        return $response;
    }

    /**
     * 删除商品
     * @RequestMapping(route="deleteGoods",method={RequestMethod::GET})
     */
    public function deleteGoods(Request $request){
        $params = $request->input();

        $client = \Elasticsearch\ClientBuilder::create()->setHosts(config('Es'))->setBasicAuthentication('elastic','changeme')->build();
        $params = [
            'index' => 'goods_list',
            'id' => $params['id']
        ];
        $response = $client->delete($params);
        return $response;
    }

    /**
     * 更新商品
     * @RequestMapping(route="updateGoods",method={RequestMethod::POST})
     */
    public function updateGoods(Request $request){
        $params = $request->input();

        $client = \Elasticsearch\ClientBuilder::create()->setHosts(config('Es'))->setBasicAuthentication('elastic','changeme')->build();
        $params = [
            'index' => 'goods_list',
            'id' => $params['id'],
            'body' => [
                'doc' => [
                    'goods_name' => $params['goods_name'],
                    'goods_description' => $params['goods_description']
                ],
            ],
        ];
        $response = $client->update($params);
        return $response;
    }

    /**
     * Elasticsearch只能查10000条数据解决办法【https://blog.csdn.net/dz77dz/article/details/124990010】
     * 全文索引【中文】
     * 文档：http://news.558idc.com/122109.html
     * @RequestMapping(route="full_text_cn",method={RequestMethod::GET})
     */
    public function fullTextCn(Request $request){
        $params = $request->input();

        $client = \Elasticsearch\ClientBuilder::create()->setHosts(config('Es'))->setBasicAuthentication('elastic','changeme')->build();
        $params = [
            'index' => 'logstash-mysql-member-log',
            'body' => [
                'query' => [
                    'bool' => [
                        'should' => [
                            [ 'match' => [ 'nickname' => [
                                'query' => $params['keyword'],
                                'boost' => 3, // 权重大
                            ]]],
                            [ 'match' => [ 'username' => [
                                'query' => $params['keyword'],
                                'boost' => 2,
                            ]]],
                        ],
                    ],
                ],
                'sort' => ['id'=>['order'=>'desc']],
                'from' => $params['from'], 'size' => $params['size']
            ]
        ];

        $response = $client->search($params);
        return $response;
    }

}